#pragma once

#include "fmod.hpp"
#pragma comment(lib, "fmodex_vc.lib")
//#pragma comment(lib, "Misc/fmodex.dll")


////////////////////////////////////////////////////////////////////////////
//// FMOD 
////////////////////////////////////////////////////////////////////////////
//// User defined FMOD error check, here we have a message box pop up.
//#define ERRCHECK(x) if(x != FMOD_OK) {MessageBox(m_hWnd, (LPCWSTR)x, L"Fmod Error", MB_OK);}

struct CSoundEngine
{

	FMOD::System*	 	system;
	FMOD_RESULT      	result;
	unsigned int     	version;
	int              	numdrivers;
	FMOD_SPEAKERMODE 	speakermode;
	FMOD_CAPS        	caps;

	char             	name[256];

	//these are the max sounds if you want more you have to make new sounds and channels arrays
	FMOD::Sound*		sound[6];
	FMOD::Channel*		channel[6];


	void initSoundEngine(HWND m_hWnd)
	{

		#define ERRCHECK(x) if(x != FMOD_OK) {MessageBox(m_hWnd, (LPCWSTR)x, L"Fmod Error", MB_OK);}
		//////////////////////////////////////////////////////////////////////////
		// Initialize FMOD and load/stream sounds
		//////////////////////////////////////////////////////////////////////////
		// The following code is taken from the "Platform Specific Issues" section 
		// and the "Getting Started" section of the FMOD documentation.

		//Use the following code as a basis for your Windows start up sequence.

		/*
		Create a System object and initialize.
		*/

		result = FMOD::System_Create(&system);
		ERRCHECK(result);

		result = system->getVersion(&version);
		ERRCHECK(result);

		if (version < FMOD_VERSION)
		{
			//printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION);
			//return 0;
		}

		result = system->getNumDrivers(&numdrivers);
		ERRCHECK(result);

		if (numdrivers == 0)
		{
			result = system->setOutput(FMOD_OUTPUTTYPE_NOSOUND);
			ERRCHECK(result);
		}
		else
		{
			result = system->getDriverCaps(0, &caps, 0, 0, &speakermode);
			ERRCHECK(result);

			result = system->setSpeakerMode(speakermode);       /* Set the user selected speaker mode. */
			ERRCHECK(result);

			if (caps & FMOD_CAPS_HARDWARE_EMULATED)             /* The user has the 'Acceleration' slider set to off!  This is really bad for latency!. */
			{                                                   /* You might want to warn the user about this. */
				result = system->setDSPBufferSize(1024, 10);
				ERRCHECK(result);
			}

			result = system->getDriverInfo(0, name, 256, 0);
			ERRCHECK(result);

			if (strstr(name, "SigmaTel"))   /* Sigmatel sound devices crackle for some reason if the format is PCM 16bit.  PCM floating point output seems to solve it. */
			{
				result = system->setSoftwareFormat(48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0,0, FMOD_DSP_RESAMPLER_LINEAR);
				ERRCHECK(result);
			}
		}

		// This initialization uses 100 'virtual voices'
		result = system->init(5, FMOD_INIT_NORMAL, 0);
		if (result == FMOD_ERR_OUTPUT_CREATEBUFFER)         /* Ok, the speaker mode selected isn't supported by this soundcard.  Switch it back to stereo... */
		{
			result = system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO);
			ERRCHECK(result);

			result = system->init(5, FMOD_INIT_NORMAL, 0);/* ... and re-init. */
			ERRCHECK(result);
		}

		result = system->createSound("Misc/crunch1.wav", FMOD_DEFAULT, 0, &sound[3]);
		ERRCHECK(result);

		result = system->createSound("Misc/Click.wav", FMOD_DEFAULT, 0, &sound[0]);
		ERRCHECK(result);


		
		//settin up sound loops

		//// Stream - Background music (looping)
		//// Start background music, once intro video has finished playing or is interrupted

		//result = system->createStream("sounds/oingobug.wav", FMOD_LOOP_NORMAL | FMOD_2D | FMOD_HARDWARE, 0, &sound[7]);

		//result = system->createStream("sounds/bumbleb1.wav", FMOD_LOOP_NORMAL | FMOD_2D | FMOD_HARDWARE, 0, &sound[5]);

		//// Start background music

		//result = system->playSound(FMOD_CHANNEL_FREE, sound[7], true, &channel[0]);
		//ERRCHECK(result);

		//result = system->playSound(FMOD_CHANNEL_FREE, sound[5], true, &channel[1]);
		//ERRCHECK(result);

		//ERRCHECK(result);

		
		////fun with sound

		//changing volume

		//bool mute = false;
		//channel[0]->setMute( mute);
		//channel[0]->setVolume(0.1f);



		//changing background sounds to pause and playing another
		//back and forth from menu to game

		////play menu music loop
		//result = channel[0]->setPaused(false);
		////pause game bugs sound loop
		////note should have this as a leaving call from gameSt play to menu
		//result = channel[1]->setPaused(true);

		////pause menu music loop
		//result = channel[0]->setPaused(true);
		////play game bugs loop
		//result = channel[1]->setPaused(false);


	}
};